﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Net;

namespace MobyGamesCrawler
{
    class Program
    {
        static void Main(string[] args)
        {
            Snes();
            Console.ReadLine();
        }

        private static void Snes()
        {
            //http://www.mobygames.com/browse/games/snes/offset,0/so,0a/list-games/
            //trocando o offset de 25 em 25
            WebClient wc = new WebClient();
            string content = wc.DownloadString("http://www.mobygames.com/browse/games/snes/offset,0/so,0a/list-games/");
            // 
            string table = Regex.Match(content, "<table id=\"mof_object_list\"(.|\n)*?</table>").Value;
            MatchCollection tr = Regex.Matches(table, "/game/snes/(.|\n)*?[a-z0-9_]\"");

            foreach (Match link in tr)
            {
                wc = new WebClient();
                string game_content = wc.DownloadString("http://www.mobygames.com" + link.Value.Replace("\"", ""));
                string nome = Regex.Replace(Regex.Match(game_content, "<div id=\"gameTitle\">(.|\n)*?</div>").Value, "<(.|\n)*?>", "");
                string div_core_game_release = Regex.Match(game_content, "<div id=\"coreGameRelease\">(.|\n)*?</a></div></div>").Value;
                string div_core_game_genre = Regex.Match(game_content, "<div id=\"coreGameGenre\">(.|\n)*?</a></div></div></div></td>").Value;
                MatchCollection release = Regex.Matches(div_core_game_release, "<a href=\"/[a-zA-Z]{0,9}(.|\n)*?</a>");
                MatchCollection genre = Regex.Matches(div_core_game_genre, "<a href=\"/[a-zA-Z]{0,9}(.|\n)*?</a>");

                string data_release = string.Empty;
                string publicador = Regex.Replace(release[0].Value, "<(.|\n)*?>", "");
                string desenvolvedor = Regex.Replace(release[1].Value, "<(.|\n)*?>", "");
                
                foreach (Match match in release)
                {
                    if (match.Value.IndexOf("release") >= 0)
                        data_release = Regex.Replace(match.Value, "<(.|\n)*?>", "");
                }

                string generos = string.Empty;
                foreach (Match match in genre)
                {
                    generos += Regex.Replace(match.Value, "<(.|\n)*?>", "");
                }

                string descricao = Regex.Match(game_content, "<h2 class=\"m5\">Description(.|\n)*?<div class=\"sideBarLinks\">").Value;
                descricao = Regex.Replace(descricao, "<(.|\n)*?>", "").Replace("Description","");

                Console.WriteLine(nome);
                System.Threading.Thread.Sleep(5000);
            }
        }

    }

    public class Jogos
    {
        public int Codigo { get; set; }
        public string Nome { get; set; }
        public int CodigoSistema { get; set; }
        public string Publicador { get; set; }
        public string Genero { get; set; }
        public string Desenvolvedor { get; set; }
        public string Descricao { get; set; }
        public DateTime DataLancamento { get; set; }

        public void Salvar()
        { 
        
        }
    }
}
